﻿using blackhouse.WebNemo.Business.Groups;
using blackhouse.WebNemo.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace blackhouse.WebNemo.Business.Files {
    public class HeaderContentType {

        #region Fields

        private RecordFile file;
        private IDataAccess dataAccess;
        private IDictionary<string, string> extraHeaders = new Dictionary<string, string>();
        private const string TABLE_NAME = "SYS_MIMETYPES";

        #endregion

        #region Properties


        /// <summary>
        /// Rozszerzenie pliku z wiodącą kropką
        /// </summary>
        public string FileExtension { get; private set; }
        public bool Exists { get; private set; }
        public string ContentType { get; private set; }
        public string ContentDescription { get; private set; }
        public long ContentLength { get { return this.file.FileSize; } }
        public IDictionary<string, string> ExtraHeaders {
            get {
                Dictionary<string, string> dd = new Dictionary<string, string>(this.extraHeaders.Count);
                foreach (KeyValuePair<string, string> entry in this.extraHeaders)
                    dd.Add(entry.Key, entry.Value);
                return dd;
            }
        }

        #endregion

        #region Constructors

        public HeaderContentType(RecordFile file) {
            this.file = file;
        }

        #endregion

        #region Methods

        private void SetDownload() {
            this.ContentType = "application/octet-stream";
            this.ContentDescription = "File Transfer";
            this.extraHeaders.Add("Content-Disposition", String.Format("attachment; filename=\"{0}\"", file.FileName));
            this.extraHeaders.Add("Content-Transfer-Encoding", "binary");
        }

        private void SetPreview() {
            this.FileExtension = this.GetFileExtension(file.FileName);
            this.Exists = !String.IsNullOrEmpty(this.FileExtension);
            if (this.Exists) {
                ContentTypeData contentData = this.FindHeaderContentType(this.FileExtension);
                this.Exists = contentData != null && this.FileExtension.Equals(contentData.FileExtension, StringComparison.InvariantCultureIgnoreCase);
                if (this.Exists) {
                    this.ContentType = contentData.MimeMediaType;
                    this.ContentDescription = contentData.TypeName;
                }
                else {
                    this.ContentType = "application/octet-stream";
                    this.ContentDescription = String.Empty;
                }
            }
        }

        private ContentTypeData FindHeaderContentType(string fileExtension) {
            IGroup group = Factory.Instance().GetGroup(TABLE_NAME);
            GroupRecordQuery q = new GroupRecordQuery(group);
            q.AddWhereClause("file_extension", fileExtension);
            GroupRecords records = new GroupRecords(group);
            return records.GetRecords<ContentTypeData>(q).FirstOrDefault();
        }

        private string GetFileExtension(string fileName) {
            string[] fParts = fileName.Split('.');
            if (fParts.Length > 1)
                return "." + fParts.Last();
            else
                return String.Empty;
        }

        public static HeaderContentType GetFromRecordFile(RecordFile file) {
            HeaderContentType hct = new HeaderContentType(file);
            hct.SetPreview();
            return hct;
        }

        #endregion

    }
}
